package com.zyb.product.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.zyb.common.annotation.LogOperation;
import com.zyb.commons.constant.Constant;
import com.zyb.commons.page.PageData;
import com.zyb.common.utils.ExcelUtils;
import com.zyb.commons.page.PageResult;
import com.zyb.commons.utils.Result;
import com.zyb.commons.validator.AssertUtils;
import com.zyb.commons.validator.ValidatorUtils;
import com.zyb.commons.validator.group.AddGroup;
import com.zyb.commons.validator.group.DefaultGroup;
import com.zyb.commons.validator.group.UpdateGroup;
import com.zyb.product.dto.AttrGroupDTO;
import com.zyb.product.entity.AttrGroupVO;
import com.zyb.product.excel.AttrGroupExcel;
import com.zyb.product.service.AttrGroupService;
import com.zyb.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 属性分组
 *
 * @author 赵毅博 593163583@qq.com
 * @since 1.0.0 2022-07-15
 */
@RestController
@RequestMapping("/product/attrgroup")
@Api(tags="属性分组")
@Slf4j

public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("page")
    @ApiOperation("分页")
    @LogOperation("查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
//    @RequiresPermissions("product:attrgroup:page")
    public Result<PageData<AttrGroupDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AttrGroupDTO> page = attrGroupService.page(params);
        return new Result<PageData<AttrGroupDTO>>().ok(page);
    }

    @PostMapping("pageQuery")
    @ApiOperation("根据catId查找对应属性")
    @LogOperation("sql实现查询")
//    @RequiresPermissions("product:attrgroup:info")
    public Result<PageResult<AttrGroupDTO>> getByCatId(@RequestBody AttrGroupVO vo){
        try {
            PageResult<AttrGroupDTO> result = attrGroupService.queryAllLimit(vo);
            return new Result<PageResult<AttrGroupDTO>>().ok(result);
        }catch (IllegalArgumentException e){
            log.error("pageNum或pageSize不能为null");
            return new Result<PageResult<AttrGroupDTO>>().error(11111,e.getMessage());
        }catch (Exception e){
            return new Result<PageResult<AttrGroupDTO>>().error(22222,"未知异常，请联系管理员");
        }
    }

    @GetMapping("{id}")
    @ApiOperation("信息")

//    @RequiresPermissions("product:attrgroup:info")
    public Result<AttrGroupDTO> get(@PathVariable("id") Long id){
        AttrGroupDTO data = attrGroupService.get(id);
        Long catelogId = data.getCatelogId();
        Long[] path = categoryService.getFatherIds(catelogId);
        data.setCatelogPath(path);
        return new Result<AttrGroupDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
//    @RequiresPermissions("product:attrgroup:save")
    public Result save(@RequestBody AttrGroupDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        attrGroupService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
//    @RequiresPermissions("product:attrgroup:update")
    public Result update(@RequestBody AttrGroupDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        attrGroupService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
//    @RequiresPermissions("product:attrgroup:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        attrGroupService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
//    @RequiresPermissions("product:attrgroup:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AttrGroupDTO> list = attrGroupService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, AttrGroupExcel.class);
    }

}